package solution._0000._0003;

/**
 * Created by chen_snow
 * Date: 2021/4/26 21:27
 * Description:
 */
public class Solution {
    public static int lengthOfLongestSubstring(String s) {
        int[] lastIndex = new int[128];
        for (int i = 0; i < 128; i++) {
            lastIndex[i] = -1;
        }

        int maxLength = 0;
        int lastLength = 0;

        for (int i = 0; i < s.length(); i++) {
            int index = lastIndex[s.charAt(i)];

            // 如果index + lastLength >= i 意味着 lastLength内有重复
            lastLength = index + lastLength >= i ? i - index : lastLength + 1;

            maxLength = Math.max(maxLength, lastLength);
            lastIndex[s.charAt(i)] = i;
        }
        return maxLength;
    }

}
